package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Accident;
import cn.wolfcode.crm.domain.ClaimBill;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.AccidentMapper;
import cn.wolfcode.crm.mapper.ClaimBillMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IClaimBillService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ClaimBillServiceImpl implements IClaimBillService{
    @Autowired
    private ClaimBillMapper mapper;
    @Autowired
    private AccidentMapper accidentMapper;
    /*
    * 保存明细时,支付状态设置为待审核
    * */
    public void save(ClaimBill entity) {
        entity.setState(ClaimBill.STATE_NOAUDIT);
        mapper.insert(entity);
    }

    public void update(ClaimBill entity) {
        mapper.updateByPrimaryKey(entity);
    }

    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public ClaimBill get(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<ClaimBill> getAll() {
        return mapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int count = mapper.queryForCount(qo);
        if(count==0){
            return new PageResult(0, Collections.emptyList());
        }
        List<ClaimBill> list = mapper.queryForList(qo);
        return new PageResult(count,list);
    }
    /*
    * 提审理赔明细,将数据状态改为待审核
    * */
    public void changeAuditState(Long id) {
        int state  = Accident.STATE_AUDIT;
        //将当前登录人员设置为验证人员
        Employee emp = (Employee) SecurityUtils.getSubject().getPrincipal();
        String validateStaff = emp.getUsername();
        accidentMapper.updateState(id,state,validateStaff);
    }
    /*
  * 拒审,删除对应的明细
  * */
    public void deleteByInsuranceNo(String insuranceNo) {
        mapper.deleteByInsuranceNo(insuranceNo);
    }
    /*
   * 拒审,将数据状态改为审核驳回
   * */
    public void changeSateByRefuse(Long id) {
        int state = Accident.STATE_REFUSE;
        accidentMapper.updateStateNoUser(id,state);
    }
    /*
    * 审核通过,支付状态设置为待支付
    * */
    public void changePayState(String insuranceNo) {
        int state = ClaimBill.STATE_NOPAY;
        mapper.changeState(insuranceNo,state);
    }
    /*
    * 确认赔付后,改变支付状态为已支付
    * */
    public void changePaiedState(String insuranceNo) {
        int state = ClaimBill.STATE_PAIED;
        mapper.changeState(insuranceNo,state);
    }
    /*
    * 根据insuranceNo来查询数据,用作回显数据
    * */
    public  List<ClaimBill> selectBillByInsuranceNo(String insuranceNo) {
         return mapper.selectBillByInsuranceNo(insuranceNo);
    }

    public ClaimBill selectByClientId(Long id) {
        return mapper.selectByClientId(id);
    }
}
